package org.moonshiner.controller.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import org.moonshiner.controller.dao.UserDao;
import org.moonshiner.model.project.Project;
import org.moonshiner.model.user.User;

@Repository
public class PgUserDaoImpl extends GenericDao<User> implements UserDao {

	@Autowired
	public PgUserDaoImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	public User getUserByUserName(String name) {

		Criteria criteria = getSession().createCriteria(User.class);
		criteria.add(Restrictions.eq("name", name));
		return (User) criteria.uniqueResult();
	}

	@Override
	public List<Project> getProjectsByUserName(String name) {

		User user = getUserByUserName(name);

		return getProjectsByUserId(user.getId());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Project> getProjectsByUserId(Integer id) {

		Criteria criteria = getSession().createCriteria(Project.class);
		criteria.add(Restrictions.eq("user.id", id));

		return (List<Project>) criteria.list();
	}

}
